<template>
  <div class="login">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <h3 class="title">登录</h3>
      <el-form-item prop="account">
        <el-input
          ref="account"
          v-model="loginForm.account"
          placeholder="请输入用户名"
          name="account"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
      </el-form-item>
      <el-form-item prop = "identity">
        <el-radio v-model="loginForm.identity" label="admin" style="color: grey">系统管理员</el-radio>
        <el-radio v-model="loginForm.identity" label="student" style="color: grey">学生</el-radio>
      </el-form-item> 

      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
        >登录</el-button
      >
      <!-- <div class="login-link">
        <span @click="showRegisterForm">没有账号？去注册</span>
      </div> -->
    </el-form>
  </div>
</template>

<script>
import { setPermission, setToken } from "@/utils/auth";
import { login } from "@/api/base";
export default {
  name: "LoginView",
  data() {
    //用户名校验规则
    const validateUsername = (rule, value, callback) => {
      if (!value || value.trim().length < 6 || value.trim().length > 16) {
        callback(new Error("用户名长度必须在6到16位之间，且不能包含空格"));
      } else {
        callback();
      }
    };

    //用户名校验规则
    const validatePassword = (rule, value, callback) => {
      if (!value || value.trim().length < 6 || value.trim().length > 16) {
        callback(new Error("密码长度必须在6到16位之间，且不能包含空格"));
      } else {
        callback();
      }
    };

    //数据模型
    return {
      loginForm: {
        account: "admin1",
        password: "admin123",
        identity: "admin",
      },
      loginRules: {
        account: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
    };
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          login(this.loginForm)
            .then((result) => {
              console.log(result);
              if (result.data.code === 200) {
                setToken(result.data.data.token);
                setPermission(result.data.data.permission);
                console.log("login success");
                this.$router.push("/index");
              } else {
                this.$message.error(result.data.msg);
                this.loading = false;
              }
            })
            .catch(() => {
              this.$message.error("登录请求失败，请稍后再试");
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // showRegisterForm() {
    //   this.$router.push("/register");
    // },
  },
};
</script>

<style lang='scss' scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('../assets/login-background.png'); 
  background-size: contain;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  width: 360px;
  padding: 40px 35px 20px 35px;
  background: rgba(255, 255, 255, 1);
  border-radius: 14px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.8);
}

.login-link {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  margin-top: 1px;
  color: #007aff;
  cursor: pointer;
  transition: color 0.3s;
  &:hover {
    color: #0056b3;
  }
}

.el-button {
  height: 40px;
  width: 100%;
  border-radius: 14px;
  background-color: #007aff;
  border-color: #007aff;
  color: #fff;
  font-size: 18px;
  font-weight: 600;
  transition: background-color 0.3s, border-color 0.3s;
  &:hover {
    background-color: #0056b3;
    border-color: #0056b3;
  }
}
</style>